package FileIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import Main.mainPanel;
import snake.Snake_Point;
import snake.Snake_body;

public class IO {
	private static File file=new File("img\\record");
	private IO(Snake_body snake)
	{
		
	}
	static public void OUT(Snake_body snake)//读入到文件中
	{
		if(snake.died) return ;
		try(FileOutputStream out=new FileOutputStream(IO.file);
			ObjectOutputStream objectout=new ObjectOutputStream(out);
				
				) {
			
			objectout.writeInt(snake.body.size());//蛇的长度
			objectout.writeInt(snake.dirction);//蛇的方向
			for(int x=0;x<snake.body.size();x++)
			{
				objectout.writeObject(snake.body.get(x));
			}
			objectout.writeDouble(mainPanel.score);
			
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	static public void IN(Snake_body snake)//从文件写到内存中
	{
		try(
				FileInputStream in=new FileInputStream(IO.file);
				ObjectInputStream objectin=new ObjectInputStream(in);
			)
		{
			int length=objectin.readInt();
			System.out.println(length);
			if(length==-1) return ;
			snake.clear();
			snake.dirction=objectin.readInt();
			for(int x=0;x<length;x++)
			{
				snake.body.add((Snake_Point) objectin.readObject());
			}
			mainPanel.score=objectin.readDouble();
		}catch(Exception e)
		{
			e.getStackTrace();
		}
		
	}
	static public void IN_SCORE_TIME()
	{
		
	}
	static public void OUT_SCORE_TIME()
	{
		
	}
}
